<?php
/**
 * Created by PhpStorm.
 * User: jacckzhang
 * Date: 2018/9/14
 * Time: 13:49
 */
namespace app\socket\controller;
use think\Env;

class  SubRedis
{
    /**
     * 架构函数
     * @access public
     */
    public function __construct()
    {
        $this->init();
    }

    public function init()
    {
        $redis = new \Redis();
        global $admin,$region;
        $admin = db('admin');
        $region = db('region');
        $redis->pconnect(Env::get('redis'.ENV.'_host'), 6379);
        $redis->setOption($redis::OPT_READ_TIMEOUT, -1);
        $redis->auth(Env::get('redis'.ENV.'_password'));
        $redis->subscribe(config('redis_channel'),function ($redis, $channel, $info)use($admin,$region){
            $msg = json_decode($info,true);
            switch($channel){
                case 'inter':
                    switch ($msg['type']){
                        case 'order':
                            $region_id = $region->alias('p')->join('region c','p.region_id=c.parent_id','LEFT')->where('c.region_code',$msg['startCode'])
                                ->column('p.parent_id as province,c.region_id as county,c.parent_id as city');
                            foreach ($region_id as $val){
                                $data[] = $val['province'];
                                $data[] = $val['county'];
                                $data[] = $val['city'];
                            }
                            $map['region_area'] = array('in',array_unique($data));
                            $map['is_css'] = 1;
                            $admin_id = $admin->alias('a')->join('admin_area aa','a.admin_id=aa.admin_id','LEFT')->where($map)
                                ->group('a.admin_id')->column('a.admin_id');
                            if(!empty($admin_id)){
                                $msg['title'] = '新的城际订单';
                                $message = array('to'=>json_encode(array()),'type'=>'publish','content'=>json_encode($msg));
                                var_dump(socketToServer($message,INNER_HTTP));
                            }
                            break;
                        default:break;
                    }
                    break;
                case 'inner':
                    switch ($msg['type']){
                        case 'alarm':
                            $region_id = $region->alias('p')->join('region c','p.region_id=c.parent_id','LEFT')->where('c.region_code',$msg['startCode'])
                                ->column('p.parent_id as province,c.region_id as county,c.parent_id as city');
                            foreach ($region_id as $val){
                                $data[] = $val['province'];
                                $data[] = $val['county'];
                                $data[] = $val['city'];
                            }
                            $map['region_area'] = array('in',array_unique($data));
                            $map['is_css'] = 1;
                            $admin_id = $admin->alias('a')->join('admin_area aa','a.admin_id=aa.admin_id','LEFT')->where($map)
                                ->group('a.admin_id')->column('a.admin_id');
                            if(!empty($admin_id)){
                                $msg['title'] = '乘客报警';
                                $message = array('to'=>json_encode(array()),'type'=>'publish','content'=>json_encode($msg));
                                var_dump(socketToServer($message,INNER_HTTP));
                            }
                            break;
                        default:break;
                    }
                    break;
                default:break;
            }
        });
    }
}